// 知和求三数
 function threeSum (nums) {
  let res = [];
  const length = nums.length;
  for (let i = 0; i < length - 2; i++) {
    let j = i + 1;
    let k = length - 1;
    if (i > 0 && nums[i] == nums[i - 1]) {
      continue;
    }
    while (j < k) {
      const sum = nums[i] + nums[j] + nums[k];
      if (sum > 0) {
        k--;
        while ((nums[k] = nums[k + 1] && j < k)) {
          k--;
        }
      } else if (sum < 0) {
        j++;
        while ((nums[j] = nums[j - 1] && j < k)) {
          j++;
        }
      } else {
        res.push(nums[i], nums[j], nums[k]);
        while (j < k && nums[j] === nums[j + 1]) {
            j++;
        }
        while (j < k && nums[k] === nums[k - 1]) {
            k--;
        }
        j++;
        k--;
      }
    }
  }
  return res;
};
// const nums = [-1, 0, 1, 2, -1, -4];
// threeSum(nums);
// console.log(threeSum(nums));
const result=threeSum( [-1, 0, 1, 2, -1, -4]);
console.log(result);

